package com.zyz.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolExecutorFactoryBean;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * 通用线程池
 */
@Configuration
public class ThreadPoolConfig {

    @Bean(name = "threadPoolExecutor")
    public ThreadPoolExecutorFactoryBean threadPoolExecutor() {
        ThreadPoolExecutorFactoryBean bean = new ThreadPoolExecutorFactoryBean();
        bean.setCorePoolSize(5);
        bean.setMaxPoolSize(20);
        bean.setKeepAliveSeconds(10);
        bean.setAllowCoreThreadTimeOut(true);
        bean.setQueueCapacity(1000);
        bean.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        return bean;
    }
}
